import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_home/model/my_device.dart';
import 'package:smart_home/router/index.dart';
import 'package:smart_home/service/my_device_service.dart';
import 'package:smart_home/style.dart';
import '../../utils/extension/double.dart';

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      initialIndex: 0,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          leadingWidth: 120.px,
          leading: Leading(),
          actions: [
            GestureDetector(
              onTap: () {
                Get.toNamed(ADDDEVICE);
              },
              child: Container(
                margin: EdgeInsets.only(right: 18.px),
                child: Icon(
                  Icons.add,
                  color: Colors.black,
                ),
              ),
            )
          ],
          bottom: _buildTabBar(),
          // title: Text("xxx的家"),
        ),
        body: TabBarView(
          children: [
            ListView(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 10.px, left: 10.px),
                  child: FutureBuilder<List<MyDevice>>(
                    future: decodeMyDevice(),
                    builder: (BuildContext context,
                        AsyncSnapshot<List<MyDevice>> snapshot) {
                      if (snapshot.connectionState == ConnectionState.done) {
                        if (snapshot.hasError) {
                          return Text("${snapshot.error}");
                        } else {
                          return GestureDetector(
                            onTap: () {
                              showBottomSheet(
                                  // transitionAnimationController: ,
                                  context: context,
                                  builder: (BuildContext context) {
                                    return Container(
                                      width: MediaQuery.of(context).size.width,
                                      color: Colors.green,
                                      height: 500.px,
                                      child: Text(
                                        "bottom sheet",
                                        style: TextStyle(color: Colors.white),
                                      ),
                                    );
                                  });
                            },
                            child: Wrap(
                              runSpacing: 10.px,
                              spacing: 20.px,
                              alignment: WrapAlignment.start,
                              children: snapshot.data!
                                  .map((e) => Column(
                                        children: [
                                          Image.asset(
                                            e.imgUrl!,
                                            width: 40.px,
                                          ),
                                          Text("${e.name}"),
                                        ],
                                      ))
                                  .toList(),
                            ),
                          );
                        }
                      } else {
                        return CircularProgressIndicator();
                      }
                    },
                  ),
                ),
              ],
            ),
            ListView(
              children: [Text("场景管理")],
            ),
          ],
        ),
      ),
    );
  }

  TabBar _buildTabBar() {
    return TabBar(
      indicator: BoxDecoration(
        color: themeColor,
        // borderRadius: BorderRadius.circular(5.px)
      ),
      labelColor: Colors.white,
      unselectedLabelColor: themeColor,
      tabs: [
        Tab(
          child: Text("已拥有设备"),
        ),
        Tab(
          child: Text("场景管理"),
        ),
      ],
    );
  }
}

class Leading extends StatelessWidget {
  const Leading({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 10.px),
      child: Row(
        children: [
          Icon(
            Icons.home,
            color: Colors.black,
          ),
          Container(
              margin: EdgeInsets.only(left: 10.px),
              child: Text("xxx的家",
                  style: TextStyle(fontSize: 16.px, color: Colors.black))),
        ],
      ),
    );
  }
}
